package baiqitun.stupro.project.core.process.impl;

import baiqitun.stupro.common.exception.enums.support.NotFoundExceptionEnum;
import baiqitun.stupro.common.exception.support.NotFoundException;
import baiqitun.stupro.common.workflow.enums.ProcessDefinitionKeyEnum;
import baiqitun.stupro.common.workflow.util.RuntimeTaskUtil;
import baiqitun.stupro.common.workflow.util.TaskUtil;
import baiqitun.stupro.project.core.process.InviteUserToProjectProcess;
import baiqitun.stupro.project.service.SysProjectService;
import baiqitun.stupro.security.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 邀请用户加入项目的流程
 * @author kedaji
 */
@Service("inviteUserToProjectProcessImpl")
public class InviteUserToProjectProcessImpl implements InviteUserToProjectProcess {
    @Autowired
    private TaskUtil taskUtil;
    @Autowired
    private RuntimeTaskUtil runtimeTaskUtil;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysProjectService sysProjectService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void sendInvitation(String projectId, String owner, String userId, String description) {
        sysProjectService.checkNotFound(projectId, new NotFoundException(NotFoundExceptionEnum.PROJECT_NOT_FOUND));
        sysUserService.checkEnabled(owner);
        sysUserService.checkEnabled(userId);
        runtimeTaskUtil.startProcessInstanceWithTask(ProcessDefinitionKeyEnum.INVITE_USER_TO_PROJECT.name(),
                projectId, owner, userId,null, description);
    }
}
